﻿/// <reference path="/Scripts/jquery-1.5.1.min.js">
///<reference path="/Scripts/Common/playListDatabase.js">
/// <reference path="jsPlayListStorage.js" />
$(document).ready(function () {
  $(".topPlay").live("click", function () {
        var id = parseInt($(this).attr("data-ID"));
        var strUrl = $(this).attr("data-action");
        var name = $(this).attr("data-Name");
        var slocation = $(this).attr("data-Location");
        var artist = $(this).attr("data-artist");
        var songID = $(this).attr("data-ID");
        jsPlayListStorage.addPlayingList(songID, name, artist, slocation);       
        openPlayer(strUrl);
    });
   
    $(".divAddSongs").live("click", function () {
        var name = $(this).attr("data-Name");
        var strUrl = $(this).attr("data-action");
        var slocation = $(this).attr("data-Location");
        var artist = $(this).attr("data-artist");
        var songID = $(this).attr("data-ID");
        jsPlayListStorage.addPlayList(songID, name, artist, slocation);
        showMsg(this,"添加成功");
        openPlayer(strUrl);
    })
    

    $(".divPlayCurrentAlbum").live("click", function () {
        var ablumid = $(this).attr("data-albumid");
        var $songsInfo = $(".topPlay[data-albumid=" + ablumid + "]");
        var strUrl = $songsInfo.eq(0).attr("data-action");
       
        for (var i = $songsInfo.length - 1; i >= 0; i--)
        {   
            var name = $songsInfo.eq(i).attr("data-Name");
            var slocation = $songsInfo.eq(i).attr("data-Location");
            var artist = $songsInfo.eq(i).attr("data-artist");
            var songID = $songsInfo.eq(i).attr("data-ID");
            jsPlayListStorage.addPlayingList(songID, name, artist, slocation);            
        }
        openPlayer(strUrl);
    });

    $("#divSearch").click(function () {
        var stext = $.trim($(".SearchQueryTextInput").val());
        if (stext == "") {
            return;
        }
        else {
            var url = $(this).attr("data-url");
            window.location = url+"/?str=" + stext;
        }
    });

   

    $(".divsongInfo,.divtopsongInfo").live({
        mouseenter: function () {
          
            $(this).addClass("hoverbackcolor");
        },
        mouseleave: function () {
            $(this).removeClass("hoverbackcolor");
            
        }
    });
    
    $(".divCollect").live("click", function () {
        var self = this;
        var ID=$(this).attr("data-ID");
        $.get("/AACInfo/Collection", { ID:ID}, function (r) {
            if (r == "false") {
                alert("您尚未登陆,暂不能使用收藏功能!");
            }
            else if(r=="exsist")
            {
                showMsg(self,"您已收藏");
            }
            else {
                showMsg(self, "收藏成功");
            }
        });
    });
    $(".cbSelectAll").live("click", function () {
        $(".cbSelectAll").attr("checked", $(this).attr("checked"));
        $(".cbSelect").attr("checked", $(this).attr("checked"));
        $(".topPlay").attr("data-selected", $(this).attr("checked"));
        $(".divAddSongs").attr("data-selected", $(this).attr("checked"));
    });
    $(".cbSelect").live("click", function () {
        $(".cbSelectAll").attr("checked", $(".cbSelect").length == $(".cbSelect:checked").length);
        $(this).parent().parent().siblings(".topPlay").attr("data-selected", $(this).attr("checked"));
        $(this).parent().parent().siblings(".divAddSongs").attr("data-selected", $(this).attr("checked"));
    });

    $(".playSelected").live("click",function () {
        var $songsInfo = $(".topPlay[data-selected=true]");
        var strUrl = $songsInfo.eq(0).attr("data-action");

        for (var i = $songsInfo.length - 1; i >= 0; i--) {
            var name = $songsInfo.eq(i).attr("data-Name");
            var slocation = $songsInfo.eq(i).attr("data-Location");
            var artist = $songsInfo.eq(i).attr("data-artist");
            var songID = $songsInfo.eq(i).attr("data-ID");
            jsPlayListStorage.addPlayingList(songID, name, artist, slocation);
        }
        openPlayer(strUrl);
    });

    $(".addSelected").live("click",function () {
        var $songsInfo = $(".divAddSongs[data-selected=true]");
        var strUrl = $songsInfo.eq(0).attr("data-action");

        for (var i = $songsInfo.length - 1; i >= 0; i--) {
            var name = $songsInfo.eq(i).attr("data-Name");
            var slocation = $songsInfo.eq(i).attr("data-Location");
            var artist = $songsInfo.eq(i).attr("data-artist");
            var songID = $songsInfo.eq(i).attr("data-ID");
            jsPlayListStorage.addPlayList(songID, name, artist, slocation);
        }
        showMsg(this,"添加成功");
        openPlayer(strUrl);
      
    });
    $(".divInput input[type=text]").keypress(function (e) {
        var code = e.keyCode;
        if (code == 13)
        {
            $("#btnSearch").click();
        }
    });
})

function openPlayer(strUrl)
{
    if (localStorage.isOpen == undefined || localStorage.isOpen == "false") {
        window.open(strUrl, "playWindow", "");
    }
}

function showMsg(obj,msg)
{
    var y = $(obj).position().top-20;
    var x = $(obj).position().left;   
    var d = new Date();
    d.toDateString();
    $("#divMsg").css("top", y + "px");
    $("#divMsg").css("left", x + "px");
    $("#divMsg").text(msg);
    $("#divMsg").fadeIn(500);
    $("#divMsg").fadeOut(1000);
}

function getPointerX(event) {
    return event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft));
}
function getPointerY(event) {
    return event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop));
}

